#include "DarkGDK.h"
#include "SC_Collision.h"

void DarkGDK( )
{
	dbSetDisplayMode( 800,600,32 );

	dbSyncOn( );
	dbSyncRate( 0 );

	dbAutoCamOff( );
	dbHideMouse( );

	//make some labels
	dbCLS( );
	dbText( 0,0,"Sphere Collision" );
	dbGetImage( 1,0,0,130,15 );
	dbCLS( );
	dbText( 0,0,"Box Collision" );
	dbGetImage( 2,0,0,110,15 );
	dbCLS( );
	dbText( 0,0,"Polygon Collision" );
	dbGetImage( 3,0,0,140,15 );
	dbCLS( );
	dbText( 0,0,"Complex Collision (Polygon)" );
	dbGetImage( 4,0,0,220,15 );
	dbCLS( );
	dbText( 0,0,"Group 1" );
	dbGetImage( 5,0,0,60,15 );
	dbCLS( );
	dbText( 0,0,"Group 2" );
	dbGetImage( 6,0,0,60,15 );
	dbCLS( );

	dbPositionCamera( 0,10,-60 );
	dbPointCamera( 0,10,0 );

	SC_Start( );

	//group 1:
	dbMakeObjectSphere( 1,10 );
	dbColorObject( 1,dbRgb(200,0,0) );
	dbPositionObject( 1,-20,25,0 );
	SC_SetupObject( 1,1,1 ); //setup object 1, into group 1, with collision type sphere (1)

	dbMakeObjectCube( 3,10 );
	dbColorObject( 3,dbRgb(200,0,0) );
	dbPositionObject( 3,-20,10,0 );
	SC_SetupObject( 3,1,2 ); //setup object 3, into group 1, with collision type box (2)

	dbMakeObjectCone( 5,10 );
	dbColorObject( 5,dbRgb(200,0,0) );
	dbPositionObject( 5,-20,-5,0 );
	SC_SetupObject( 5,1,0 ); //setup object 5, into group 1, with collision type polygon (0)

	dbMakeObjectSphere( 7,10,50,50 ); //(approx 5000 polygons)

	dbColorObject( 7,dbRgb(200,0,0) );
	dbPositionObject( 7,-20,-20,0 );
	SC_SetupComplexObject( 7,1,2 ); //setup object 7 as a complex object, into group 1, with 2 faces per tree node (explained later)

	//group 2:
	dbMakeObjectSphere( 2,10 );
	dbColorObject( 2,dbRgb(0,200,0) );
	dbPositionObject( 2,20,25,0 );
	SC_SetupObject( 2,2,1 ); //setup object 2, into group 2, with collision type sphere (1)

	dbMakeObjectCube( 4,10 );
	dbColorObject( 4,dbRgb(0,200,0) );
	dbPositionObject( 4,20,10,0 );
	SC_SetupObject( 4,2,2 ); //setup object 4, into group 2, with collision type box (2)

	dbMakeObjectCone( 6,10 );
	dbColorObject( 6,dbRgb(0,200,0) );
	dbPositionObject( 6,20,-5,0 );
	SC_SetupObject( 6,2,0 ); //setup object 6, into group 2, with collision type polygon (0)

	dbMakeObjectSphere( 8,10,50,50 ); //(approx 5000 polygons)
	dbColorObject( 8,dbRgb(0,200,0) );
	dbPositionObject( 8,20,-20,0 );
	SC_SetupComplexObject( 8,2,2 ); //setup object 8 as a complex object, into group 2, with 2 faces per tree node (explained later)

	//makeColorImage(1,7,rgb(255,255,255))

	//make some bullet markers
	for( int i=10002; i <= 10040; i++ )
	{
		dbMakeObjectPlain( i,3,3 );
		//texture object i,7
		dbSetObjectLight( i,0 );
		dbHideObject( i );
	}

	//make the labels
	dbMakeObjectPlain( 100,15,1.5 );
	dbTextureObject( 100,1 );
	dbGhostObjectOn( 100 );
	dbPositionObject( 100,0,25,0 );

	dbMakeObjectPlain( 101,11,1.5 );
	dbTextureObject( 101,2 );
	dbGhostObjectOn( 101 );
	dbPositionObject( 101,0,10,0 );

	dbMakeObjectPlain( 102,14,1.5 );
	dbTextureObject( 102,3 );
	dbGhostObjectOn( 102 );
	dbPositionObject( 102,0,-5,0 );

	dbMakeObjectPlain( 103,22,1.5 );
	dbTextureObject( 103,4 );
	dbGhostObjectOn( 103 );
	dbPositionObject( 103,0,-20,0 );

	dbMakeObjectPlain( 104,6,1.5 );
	dbTextureObject( 104,5 );
	dbGhostObjectOn( 104 );
	dbPositionObject( 104,-20,35,0 );

	dbMakeObjectPlain( 105,6,1.5 );
	dbTextureObject( 105,6 );
	dbGhostObjectOn( 105 );
	dbPositionObject( 105,20,35,0 );

	//show object 10002

	int counter = 10002;
	int igroup = 0;
	char group [ 32 ] = "all groups";
	
	int rotatetimer = dbTimer( );
	int ttimer = dbTimer( );
	int switchGroup = dbTimer( );

	int collide = 0;
	int toggletext = 0;

	while( LoopGDK( ) )
	{
		dbYRotateCamera( dbCameraAngleY() + ( dbMouseMoveX()/10.0f ) );
		dbXRotateCamera( dbCameraAngleX() + ( dbMouseMoveY()/10.0f ) );
		dbPositionMouse( 400,300 );
		
		if ( dbSpaceKey() == 1 && rotatetimer < dbTimer() )
		{
			rotatetimer = dbTimer() + 300;
			
			for( int i = 1; i <= 8; i++ )
			{
				dbRotateObject( i,(float)dbRnd(359),(float)dbRnd(359),(float)dbRnd(359) );
				//UPDATE -  update our object
				SC_UpdateObject( i );
			}
			
			for( int i = 10002; i <= 10040; i++ )
			{
				dbHideObject( i );
			}
		}
		
		if ( dbScanCode() == 20 && ttimer < dbTimer() )
		{
			ttimer = dbTimer() + 300;
			toggletext = 1 - toggletext;
		}
		
		if ( dbReturnKey() == 1 && switchGroup < dbTimer() )
		{
			switchGroup = dbTimer() + 300;
			
			igroup = igroup + 1;
			if ( igroup > 2 ) igroup = 0;
			
			if ( igroup == 0 ) sprintf_s( group, 32, "all groups" );
			if ( igroup == 1 ) sprintf_s( group, 32, "group 1" );
			if ( igroup == 2 ) sprintf_s( group, 32, "group 2" );
		}
		
		if ( dbMouseClick() == 1 )
		{			
			//get our collision vector
			float oldx = dbCameraPositionX();
			float oldy = dbCameraPositionY();
			float oldz = dbCameraPositionZ();
			dbMoveCamera( 2000 );
			float x = dbCameraPositionX();
			float y = dbCameraPositionY();
			float z = dbCameraPositionZ();
			dbMoveCamera( -2000 );
			
			collide = SC_RayCastGroup( igroup, oldx,oldy,oldz, x,y,z, 0 );
			
			if ( collide > 0 )
			{
				//get the collision point			
				float newx = SC_GetStaticCollisionX();
				float newy = SC_GetStaticCollisionY();
				float newz = SC_GetStaticCollisionZ();
				//get collision normal
				float normx = SC_GetCollisionNormalX();
				float normy = SC_GetCollisionNormalY();
				float normz = SC_GetCollisionNormalZ();
				
				//position and point a marker in the right direction
				dbPositionObject( counter, newx + normx/10.0f, newy + normy/10.0f, newz + normz/10.0f );
				dbPointObject( counter, newx + normx, newy + normy, newz + normz );
				dbShowObject( counter );
				counter++;
				if ( counter > 10040 ) counter = 10002;
			}
		}

		char str [ 256 ];
		
		sprintf_s( str, 256, "Currently cheking for collision with: %s", group );	dbText( 0,0,  str );
		sprintf_s( str, 256, "FPS: %d, Polys: %d", dbScreenFPS(), dbStatistic(1) );	dbText( 0,20, str );
		sprintf_s( str, 256, "Last Collision Object: %d", collide );				dbText( 0,40, str );
		if ( toggletext == 0 )
		{
		   dbText( 0,60,"Move mouse to aim, left click to shoot" );
		   dbText( 0,80,"Press space to randomly rotate objects" );
		   dbText( 0,100,"Press Esc to quit" );
		   dbText( 0,120,"Press return to change groups" );
		   dbText( 0,140,"Press t to toggle this text" );
		}
		
		dbInk( dbRgb(255,0,0),0 );
		dbCircle( 400,300,10 );
		dbDot( 400,300 );
		dbInk( dbRgb(255,255,255),0 );
		
		dbSync( );
	}
}